<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Start of file administrator.php */
/* Location: ./billing-ap/controllers/administrator.php */
class Administrator extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('providers'); /** load table providers **/ 
        $this->load->model('excels'); /** load function excels **/
        if(!$this->session->userdata('username')) /** restrict area if not acces to login page**/
			redirect('/',301); 
    }
	
	function gas_distribution(){
		$this->ws->set('title',lang('gas_distribution'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('provider_mbenet',$this->providers->getDataProviderMbenet()); /** Get Data Table ProviderMbenet*/
		$this->ws->set_theme('smooth'); /** set theme to administrator*/
		$this->ws->view('gas-distribution/index'); /** view layout to views/gas-disribution/index*/
	}
    
    function get_provider(){
        /** Variabel Input Post **/
        $provider = $this->input->post('provider');/** transalate to variabel provider post provider**/
        $month    = $this->input->post('month');/** transalate to variabel month post month**/
        $year     = $this->input->post('year');/** transalate to variabel year post year**/
        /** if provider Odira or Odira ke BBG **/
        if($provider == "Odira" || $provider == "Odira ke BBG" )
	       $page   =   "index_11";
        /** if provider Pertamina EP atau PTM **/   
        elseif(($provider == "Pertamina EP") || ($provider == "PTM"))
        	$page   =  "index_7";
        /** if provider Mutiara Energy or PT Mutiara Energy **/    
        elseif(($provider == "Mutiara Energy")|| ($provider == "PT. Mutiara Energy" ))
        	$page   =  "index_6";
        /** if Pertiwi Nusantara Resources or PNR **/    
        elseif(($provider == "PT. Pertiwi Nusantarta Resouces") || ($provider == "PNR") || ($provider == "PNR Kandanghaur Timur") || ($provider == "PNR Pamanukan"))
        	$page   =  "index_5";
        /** if provider Pondok Tengah or PTG **/
        else if($provider == "Pondok Tengah" || $provider == "PTG" || $provider == "PDT ke BBG")
        	$page   =  "index_13";
        /** if Other **/    
        else
        	$page   =  "index_4";
        
        /** Insert to Session Data **/
        $value  =   array('page'=>$page,'provider'=>$provider,'month_provider'=>$month,'year_provider'=>$year);
        $this->session->set_userdata($value);
        redirect('billing-ap/administrator/provider',301); /** Redirect to Provider **/   
    }
    
    function provider(){
        $this->ws->set('title',lang('gas_distribution'));/** title page */
        $this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('provider');
        $month      =   $this->session->userdata('month_provider');
        $year       =   $this->session->userdata('year_provider');
   	    $this->ws->set('provider',$provider);/** title page */
        $this->ws->set('month',$month);/** month provider */
        $this->ws->set('year',$year);/** title page */
        $this->ws->set('recordset',$this->providers->getDailyLogProvider($month,$year,$provider)); /** Get Data Table ProviderMbenet*/
        $this->ws->set_theme('smooth'); /** set theme to administrator*/
		$this->ws->view('gas-distribution/'.$this->session->userdata('page')); /** view layout to views/gas-disribution/page of provider */
	}
    
    function getProviderReset(){
        $value  =   array('page','provider'); /** page,provider **/     
        $this->session->unset_userdata($value); /** Unset Session Provider **/
        redirect('billing-ap/administrator/gas_distribution',301);/** Redirect to gas distribution function **/
    }
    
    function getExport(){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('provider');
        $month      =   $this->session->userdata('month_provider');
        $year       =   $this->session->userdata('year_provider');
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('billing-ap/administrator/getProviderExcel/'.$month.'/'.$year.'/'.$provider); 
        elseif($this->input->post('export')=='pdf')    
            redirect('billing-ap/administrator/getProviderPdf/'.$month.'/'.$year.'/'.$provider);
        elseif($this->input->post('export')=='email')    
            redirect('billing-ap/administrator/getProviderEmail/'.$month.'/'.$year.'/'.$provider);
		else
			echo 'Error redirecting';
    }
    
    function getProviderExcel($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('provider'); /** session provider*/
        $month      =   $this->session->userdata('month_provider'); /** session month*/
        $year       =   $this->session->userdata('year_provider'); /** session year*/   
        
		/** if provider Odira or Odira ke BBG **/
        if($provider == "Odira" || $provider == "Odira ke BBG" )
	       $file   =   $this->excels->index_11($month,$year,$provider); /** file */
        /** if provider Pertamina EP atau PTM **/   
        elseif(($provider == "Pertamina EP") || ($provider == "PTM"))
        	$file   =   $this->excels->index_7($month,$year,$provider); /** file */
        /** if provider Mutiara Energy or PT Mutiara Energy **/    
        elseif(($provider == "Mutiara Energy")|| ($provider == "PT. Mutiara Energy" ))
        	$file   =   $this->excels->index_6($month,$year,$provider); /** file */
        /** if Pertiwi Nusantara Resources or PNR **/    
        elseif(($provider == "PT. Pertiwi Nusantarta Resouces") || ($provider == "PNR") || ($provider == "PNR Kandanghaur Timur") || ($provider == "PNR Pamanukan"))
        	$file   =   $this->excels->index_5($month,$year,$provider); /** file */
        /** if provider Pondok Tengah or PTG **/
        else if($provider == "Pondok Tengah" || $provider == "PTG" || $provider == "PDT ke BBG")
        	$file   =   $this->excels->index_13($month,$year,$provider); /** file */
        /** if Other **/    
        else
        	$file   =   $this->excels->index_4($month,$year,$provider); /** file */
		
        $value      =   file_get_contents($file); /** Read the file's contents **/
        $name       =   'gas.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getProviderPdf($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('provider'); /** session provider*/
        $month      =   $this->session->userdata('month_provider'); /** session month*/
        $year       =   $this->session->userdata('year_provider'); /** session year*/
        
        /** if provider Odira or Odira ke BBG **/
        if($provider == "Odira" || $provider == "Odira ke BBG" )
	       $pdf     =   'pdf_11'; /** get file  from pdf */
        /** if provider Pertamina EP atau PTM **/   
        elseif(($provider == "Pertamina EP") || ($provider == "PTM"))
       	    $pdf    =   'pdf_5'; /** get file  from pdf */
        /** if provider Mutiara Energy or PT Mutiara Energy **/    
        elseif(($provider == "Mutiara Energy")|| ($provider == "PT. Mutiara Energy" ))
        	$pdf    =   'pdf_6'; /** get file  from pdf */
        /** if Pertiwi Nusantara Resources or PNR **/    
        elseif(($provider == "PT. Pertiwi Nusantarta Resouces") || ($provider == "PNR") || ($provider == "PNR Kandanghaur Timur") || ($provider == "PNR Pamanukan"))
        	$pdf    =   'pdf_4'; /** get file  from pdf */
        /** if provider Pondok Tengah or PTG **/
        else if($provider == "Pondok Tengah" || $provider == "PTG" || $provider == "PDT ke BBG")
        	$pdf    =   'pdf_13'; /** get file  from pdf */
        /** if Other **/    
        else
        	$pdf    =   'pdf_4'; /** get file from pdf */
        
        $this->ws->set('provider',$provider); /** set to Provider **/
        $this->ws->set('month',$month); /** set to Month **/
        $this->ws->set('year',$year); /** set to Year **/
		$this->ws->set('recordset',$this->providers->getDailyLogProvider($month,$year,$provider)); /** Get Data Table ProviderMbenet*/   
        $this->ws->set_theme('smooth-report'); /** set theme to administrator*/
		$data = $this->ws->view('gas-distribution/'.$pdf,'',TRUE); /** view layout to views/gas-disribution/page of provider */
        pdf_create($data,'Gas'.$provider);
    }
    
    function getproviderEmail($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('provider'); /** session provider*/
        $month      =   $this->session->userdata('month_provider'); /** session month*/
        $year       =   $this->session->userdata('year_provider'); /** session year*/
        
        /** if provider Odira or Odira ke BBG **/
        if($provider == "Odira" || $provider == "Odira ke BBG" )
	       $excel          =   $this->excels->index_11($month,$year,$provider); /** get file  from excel download */
        /** if provider Pertamina EP atau PTM **/   
        elseif(($provider == "Pertamina EP") || ($provider == "PTM"))
       	    $excel          =   $this->excels->index_5($month,$year,$provider); /** get file  from excel download */
        /** if provider Mutiara Energy or PT Mutiara Energy **/    
        elseif(($provider == "Mutiara Energy")|| ($provider == "PT. Mutiara Energy" ))
        	$excel          =   $this->excels->index_6($month,$year,$provider); /** get file  from excel download */
        /** if Pertiwi Nusantara Resources or PNR **/    
        elseif(($provider == "PT. Pertiwi Nusantarta Resouces") || ($provider == "PNR") || ($provider == "PNR Kandanghaur Timur") || ($provider == "PNR Pamanukan"))
        	$excel          =   $this->excels->index_4($month,$year,$provider); /** get file  from excel download */
        /** if provider Pondok Tengah or PTG **/
        else if($provider == "Pondok Tengah" || $provider == "PTG" || $provider == "PDT ke BBG")
        	$excel          =   $this->excels->index_13($month,$year,$provider); /** get file  from excel download */
        /** if Other **/    
        else
        	$excel          =   $this->excels->index_11($month,$year,$provider); /** get file  from excel download */
            
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'BBG SAF Report Penyaluran Gas Dari'.$provider." Periode ". $month .' '.$year;
        $contents       = 'Dilampirkan File Penyaluran Gas '.$provider." Periode ". $month .' '.$year;
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('billing-ap/administrator/provider',301);
    }
    
    function recap_distribution(){
        $this->ws->set('title',lang('recap_distribution'));/** title recap distribution page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $month    =  $this->session->userdata('recap_month');
        $year     =  $this->session->userdata('recap_year');
        $provider =  $this->session->userdata('recap_provider');
        $this->ws->set('month',$this->session->userdata('recap_month')); /** month session */
		$this->ws->set('year',$this->session->userdata('recap_year')); /** year session */
		$this->ws->set('provider',$this->session->userdata('recap_provider')); /** provider session*/
        $this->ws->set('providers',$this->providers->getDailyLogProvider($month,$year,$provider));
        $this->ws->set('provider_mbenet',$this->providers->getDataProviderMbenetPerGroup()); /** Get Data Table ProviderMbenet*/
		$this->ws->set_theme('smooth'); /** set theme to smooth administrator*/
		$this->ws->view('recap-distribution/index'); /** view layout to views/recap-disribution/index*/
    }
    
    function getRecapProvider(){
        /** page,provider **/
        $value  =   array(  'recap_month'   =>  $this->input->post('month'),
                            'recap_year'    =>  $this->input->post('year'),
                            'recap_provider'=>  $this->input->post('provider')); 
        $this->session->set_userdata($value); /** Unset Session Provider **/
        redirect('billing-ap/administrator/recap_distribution',301);/** Redirect to recap distribution function **/
    }
    
    function getRecapExport(){  
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('provider');
        $month      =   $this->session->userdata('month_provider');
        $year       =   $this->session->userdata('year_provider');
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('billing-ap/administrator/getRecapProviderExcel/'.$month.'/'.$year.'/'.$provider); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('billing-ap/administrator/getRecapProviderPdf/'.$month.'/'.$year.'/'.$provider);
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('billing-ap/administrator/getRecapProviderEmail/'.$month.'/'.$year.'/'.$provider);
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getRecapProviderEmail($month,$year,$provider){
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'Rekap Penyaluran Gas Dari'.$provider." Periode ". $month .' '.$year;
        $contents       = 'Dilampirkan File Rekap Penyaluran Gas '.$provider." Periode ". $month .' '.$year;
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $excel          =   $this->excels->recap_distribution($month,$year,$provider); /** get file  from excel download */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('billing-ap/administrator/provider',301);
    }
    
    function getRecapProviderExcel($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('recap_provider'); /** session provider*/
        $month      =   $this->session->userdata('recap_month'); /** session month*/
        $year       =   $this->session->userdata('recap_year'); /** session year*/   
       	$file       =   $this->excels->recap_distribution($month,$year,$provider); /** file */
        $value      =   file_get_contents($file); /** Read the file's contents **/
        $name       =   'rekap-gas.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getRecapProviderPdf($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('recap_provider'); /** session provider*/
        $month      =   $this->session->userdata('recap_month'); /** session month*/
        $year       =   $this->session->userdata('recap_year'); /** session year*/
        $this->ws->set('provider',$provider); /** set to Provider **/
        $this->ws->set('month',$month); /** set to Month **/
        $this->ws->set('year',$year); /** set to Year **/
		$this->ws->set('recordset',$this->providers->getDailyLogProvider($month,$year,$provider)); /** Get Data Table ProviderMbenet*/   
        $this->ws->set_theme('smooth-report'); /** set theme to administrator*/
		$data = $this->ws->view('recap-distribution/pdf','',TRUE); /** view layout to views/gas-disribution/page of provider */
        pdf_create($data,'Rekap Gas-'.$provider); /** Create a pdf Document **/
    }
    
    function getRecapProviderReset(){
        $value  = array('recap_month'=>'recap_month','recap_year'=>'recap_year','recap_provider'=>'recap_provider'); /** value **/
        $this->session->unset_userdata($value); /** unset userdata value **/
        redirect('billing-ap/administrator/recap_distribution',301); /** redirect to recap distribution **/  
    }
    
    function gas_reception(){
        $this->ws->set('title',lang('gas_reception'));/** title gas reception page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $month    =  $this->session->userdata('reception_month'); /** Month Session**/
        $year     =  $this->session->userdata('reception_year'); /** Year Session**/
        $provider =  $this->session->userdata('reception_provider'); /** Provider Session**/
        
        /** if provider EHK Bitung **/
        if($provider == "EHK Bitung")
	       $page    =   "index_03";
        /** if provider Fajar Paper **/   
        elseif($provider == "Fajar Paper")
        	$page   =  "index_02";   
        /** if Other **/    
        else
        	$page   =  "index";
 
        $this->ws->set('month',$month); /** month session */
		$this->ws->set('year',$year); /** year session */
		$this->ws->set('provider',$provider); /** provider session*/
        $this->ws->set('providers',$this->providers->getDailyLogProvider($month,$year,$provider));
        $this->ws->set('provider_mbenet',$this->providers->getDataProviderMbenetPerGroup()); /** Get Data Table ProviderMbenet*/
		$this->ws->set_theme('smooth'); /** set theme to smooth administrator*/
		$this->ws->view('gas-reception/'.$page); /** view layout to views/recap-disribution/index*/
    }
    
    function getReceptionProvider(){
        /** page,provider **/
        $value  =   array(  'reception_month'   =>  $this->input->post('month'),
                            'reception_year'    =>  $this->input->post('year'),
                            'reception_provider'=>  $this->input->post('provider')); 
        $this->session->set_userdata($value); /** Unset Session Provider **/
        redirect('billing-ap/administrator/gas_reception',301);/** Redirect to recap distribution function **/
    }
    
    function getReceptionExport(){  
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('reception_provider');
        $month      =   $this->session->userdata('reception_month');
        $year       =   $this->session->userdata('reception_year');
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('billing-ap/administrator/getReceptionProviderExcel/'.$month.'/'.$year.'/'.$provider); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('billing-ap/administrator/getReceptionProviderPdf/'.$month.'/'.$year.'/'.$provider);
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('billing-ap/administrator/getReceptionProviderEmail/'.$month.'/'.$year.'/'.$provider);
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getResetReceptionProvider(){
        $value  = array('reception_month'=>'reception_month','reception_year'=>'reception_year','reception_provider'=>'reception_provider'); /** value **/
        $this->session->unset_userdata($value); /** unset userdata value **/
        redirect('billing-ap/administrator/gas_reception',301); /** redirect to recap distribution **/  
    }
    
    function getReceptionProviderPdf($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('reception_provider'); /** session provider*/
        $month      =   $this->session->userdata('reception_month'); /** session month*/
        $year       =   $this->session->userdata('reception_year'); /** session year*/
        /** if provider EHK Bitung **/
        if($provider == "EHK Bitung")
	       $pdf    =   "pdf_03";
        /** if provider Fajar Paper **/   
        elseif($provider == "Fajar Paper")
        	$pdf   =  "pdf_02";   
        /** if Other **/    
        else
        	$pdf   =  "pdf";
        $this->ws->set('provider',$provider); /** set to Provider **/
        $this->ws->set('month',$month); /** set to Month **/
        $this->ws->set('year',$year); /** set to Year **/
		$this->ws->set('recordset',$this->providers->getDailyLogProvider($month,$year,$provider)); /** Get Data Table ProviderMbenet*/   
        $this->ws->set_theme('smooth-report'); /** set theme to administrator*/
		$data = $this->ws->view('gas-reception/'.$pdf,'',TRUE); /** view layout to views/gas-disribution/page of provider */
        pdf_create($data,'Penerimaan Gas-'.$provider); /** Create a pdf Document **/
    }
    
    function getReceptionProviderExcel($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('reception_provider'); /** session provider*/
        $month      =   $this->session->userdata('reception_month'); /** session month*/
        $year       =   $this->session->userdata('reception_year'); /** session year*/   
        
		/** if provider EHK Bitung **/
        if($provider == "EHK Bitung" )
            $excel  =   $this->excels->reception_03($month,$year,$provider); /** file */
        /** if provider Pertamina EP atau PTM **/   
        elseif(($provider == "Fajar Paper"))
       	    $excel  =  $this->excels->reception_02($month,$year,$provider); /** file */
        else
        	$excel  =  $this->excels->reception($month,$year,$provider); /** file */
        $value      =   file_get_contents($excel); /** Read the file's contents **/
        $name       =   'penerimaan-gas.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getReceptionproviderEmail($month,$year,$provider){
        /** Provider Variabel **/
        $provider   =   $this->session->userdata('reception_provider'); /** session provider*/
        $month      =   $this->session->userdata('reception_month'); /** session month*/
        $year       =   $this->session->userdata('reception_year'); /** session year*/   
        
        /** if provider EHK Bitung **/
        if($provider == "EHK Bitung")
	       $excel  =   $this->excels->reception_03($month,$year,$provider); /** file **/
        /** if provider Fajar Paper **/   
        elseif($provider == "Fajar Paper")
        	$excel  =  $this->excels->reception_02($month,$year,$provider); /** file **/ 
        /** if Other **/     
        else
        	$excel  =  $this->excels->reception($month,$year,$provider); /** file **/
            
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'BBG SAF Report Penyaluran Gas Dari'.$provider." Periode ". $month .' '.$year;
        $contents       = 'Dilampirkan File Penyaluran Gas '.$provider." Periode ". $month .' '.$year;
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('billing-ap/administrator/gas-reception',301);
    }
       
}
/* Start of file administrator.php */
/* Location: ./billing-ap/controllers/administrator.php */